import json
import os
import time

import requests
import pytest
import yaml

from common import yaml_util
import sys
sys.path.append(r'C:\Users\admin\PycharmProjects\pythonProject\Python')
from request_Util import Requests_Util
from pytest import assume
from string import Template
# 问题：python 报错：Empty suite
# 原因：
# 1.类名用Test命名
# 2.代码中使用了main函数调用类中的内容
# 所以解决办法：只要不满足上面的某一条件就行
class Test_Flag:
    # @pytest.mark.skipif
    # def test_one(self):
    #     print(os.getcwd())
    # @pytest.mark.skipif(reason='执行跳过')
    @pytest.mark.parametrize('caseinfo',yaml_util.Yaml_Util().read_testcase_yaml('get_token.yml'))
    def test_login(self,caseinfo):#得到的caseinfo是字典格式
        res = Requests_Util().send_request(caseinfo['request']['method'],caseinfo['request']['url'],caseinfo['request']['header'],caseinfo['request']['data'])
        result = json.loads(res)
        token = result['result']['token']
        # assert '00000' in result['ret']
        # pytest.assume('00006' in result['ret'])
        with assume:assert caseinfo['assert'] in result['ret']
        # yaml_util.Yaml_Util().write_extract({'token':result['result']['token']})
        yaml_util.Yaml_Util().write_extract({'content-type':'application/json','accept': '*/*','Authorization':token})

    @pytest.mark.parametrize('caseinfo',yaml_util.Yaml_Util().read_testcase_yaml('create_flag.yml'))
    # @pytest.mark.parametrize('header',yaml_util.Yaml_Util().read_testcase_yaml('extract.yml'))
    def test_createflag(self,caseinfo,header_yaml):
        res = Requests_Util().send_request(caseinfo['request']['method'],caseinfo['request']['url'],header_yaml,caseinfo['request']['data'])
        result = json.loads(res)
        # print('创建的Flag的ID',json.loads(res)['result'])
        yaml_util.Yaml_Util().write_extract({'flagId':json.loads(res)['result']})
        with assume:assert caseinfo['assert'] in result['ret']
        # with open('change_money.yml',encoding='utf-8') as fp:
        #     read_yml_str = fp.read()
        #     tempTemplater = Template(read_yml_str)
        #     c = tempTemplater.safe_substitute({"flagId": json.loads(res)['result']})
        #     print(c)
        # yaml_data = yaml.safe_load(c)
        # print(yaml_data)
#
#         # with open('change_money.yml','r',encoding='utf-8') as f:
#         #     doc = yaml.load(f,yaml.SafeLoader)
#         # print(type(doc),doc,doc[0]['data'])
#         # doc[0]['data']['flagId'] = json.loads(res)['result']
#         # with open('change_money.yml', 'w') as f:
#         #     yaml.safe_dump(doc, f, default_flow_style=False)
    @pytest.mark.parametrize('caseinfo',yaml_util.Yaml_Util().read_testcase_yaml('change_money.yml'))
    def test_paychallengemoney(self,caseinfo,header_yaml,header_yaml_flagId):
        # header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization':yaml_util.Yaml_Util().read_extract('token')}
        # url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/pay/setChallengeMoney'
        # print(header_yaml,header_yaml_flagId)
        caseinfo['data']['flagId'] = header_yaml_flagId
        # print(caseinfo['data'])
        # data = {
        #     "challengeMoney":100,
        #     "flagId":yaml_util.Yaml_Util().read_extract('flagId'),
        #     "payType":2
        # }
        res = requests.request('post',url=caseinfo['url'],headers=header_yaml,json=caseinfo['data'])
        result = res.json()
        with assume:assert '00000' in result['ret']
#     # @pytest.mark.parametrize('caseinfo',yaml_util.Yaml_Util().read_testcase_yaml('ca.yml'))
#     # def test_paychallengemoney_eeror(self,caseinfo,header_yaml,header_yaml_flagId):
#     #     res = requests.request('post',url=caseinfo['url'],headers=header_yaml,json=caseinfo['data'])
#     #     result = res.json()
#     #     with assume:assert '00000' in result['ret']
    # 上传图片
    def test_fileupload(self,header_yaml):
        del header_yaml['content-type']
        # header = {'accept':'*/*','Authorization':yaml_util.Yaml_Util().read_extract('Authorization')}
        url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/file/upload?fileType=IMAGE'
        data = {
            "file":('1.jpg',open(r'C:\Users\admin\Desktop\新建文件夹\1.jpg','rb'),'image/jpeg')
        }
        res = requests.request('post',url=url,files=data,headers=header_yaml)
        yaml_util.Yaml_Util().write_extract({'httpPic':res.json()})
        assert '00000' == res.json()['ret']
        return(res.json())
#
#     def test_punchin(self,header_yaml):
#         # print(yaml_util.Yaml_Util().read_extract('flagId'),yaml_util.Yaml_Util().read_extract('httpPic')['result'])
#         # header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization':yaml_util.Yaml_Util().read_extract('token')}
#         url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/clockInRecord/punchIn'
#         data = {
#             "content": "接口自动化打卡",
#             "flagId":yaml_util.Yaml_Util().read_extract('flagId'),
#             "pictureList":[yaml_util.Yaml_Util().read_extract('httpPic')['result']],
#             "type":3,
#             "userId":5
#         }
#         res = requests.request('post', url=url, json=data, headers=header_yaml)
#         # print(res.json())
#         # assert '00000' == res.json()['ret']
#
# if __name__ == '__main__':
    pytest.main(['-vs'])
